/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.utils
{
	import com.puny.mvc.models.Model;
	import com.puny.utils.easing.OneEndsTrigonometricEasing;
	
	import flash.display.Bitmap;
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;

	public class Explosion extends Distributor
	{
		private var _gaussian:IGaussian;
		/**
		 * 
		 * @param obj
		 * @param type Distributor.EXHALE or Distributor.INHALE
		 * @param frame
		 * @param radius
		 * @param size
		 * @param bg
		 * 
		 */
		public function Explosion(obj:DisplayObject, type:int, frame:int, radius:Number, size:int=1, bg:uint=0xFFFFFF)
		{
			_gaussian = Random.getGaussian(radius, radius * .85);
			super(getInternalClass(), obj, type, frame, size, bg);
		}

		override protected function getModel(x:int,y:int,rect:Rectangle,bmp:Bitmap):Model {
			var model:Model = new Model();
			var r:Number = _gaussian.float;
			var t:Number = Random.between(0,Math.PI * 2);
			model.bitmap = bmp;
			if (type == INHALE) {
				model.bitmap.x = r*Math.cos(t) + halfWidth;
				model.bitmap.y = r*Math.sin(t) + halfHeight;

				model.easingX = new OneEndsTrigonometricEasing(model.bitmap.x, rect.x+x, frame,1);
				model.easingY = new OneEndsTrigonometricEasing(model.bitmap.y, rect.y+y, frame,1);
			} else if (type == EXHALE) {
				model.bitmap.x = rect.x+x;
				model.bitmap.y = rect.y+y;

				model.easingX = new OneEndsTrigonometricEasing(model.bitmap.x, r*Math.cos(t)+ halfWidth , frame,1);
				model.easingY = new OneEndsTrigonometricEasing(model.bitmap.y, r*Math.sin(t)+ halfHeight, frame,1);
			}
			return model;
		}
		
		override protected function uniqueAction(model:Model):void {
			if (type == EXHALE) {
				model.bitmap.alpha -= 1/frame;
			}
		}
		
	}
}